#-*-coding:utf-8-*-
'''
Created on 2009/6/5

@author: chenhh
'''
import urllib
import urllib2
import string
import fetchstockid
import time
import psyco

if __name__ == '__main__':
    psyco.full()
    stock_ids = fetchstockid.fetchTWSEStockID()
    t1 = time.clock()
    for cnt, id in enumerate(stock_ids):
        for year in xrange(1995, 2009):
            for month in xrange(1,13):
                formatedMon = string.zfill(month, 2)
                url = "http://www.twse.com.tw/ch/trading/exchange/STOCK_DAY/genpage/Report%s%s/%s%s_F3_1_8_%s.php?STK_NO=%s&myear=%s&mmon=%s"\
                    %(year,  formatedMon, year,  formatedMon,id, id, year,  formatedMon)
                #print url
                response = urllib2.urlopen(url)
                content = response.read()
                filename = "/media/disk-1/stock_data/%s_%s%s.htm"%(id, year, formatedMon)
                file = open(filename, 'w')
                file.write(content)
                file.close()
                print '[%.2f] percentage : %s download complete'%(float(cnt)/len(stock_ids)*100, filename)
    t2 = time.clock()
    print "total time = %s secs"%(t2-t1)
   
    
    
     